/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.cms.contenttype.service;

import com.jeeplus.cms.util.CmsCacheUtils;
import com.jeeplus.cms.util.CmsUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.cms.column.entity.CmsColumn;
import com.jeeplus.modules.cms.contenttype.entity.CmsContentType;
import com.jeeplus.modules.cms.contenttype.mapper.CmsContentTypeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 栏目内容分类Service
 *
 * @author Sawyer
 * @version 2020-11-22
 */
@Service
@Transactional(readOnly = true)
public class CmsContentTypeService extends CrudService<CmsContentTypeMapper, CmsContentType> {

    @Override
    public CmsContentType get(String id) {
        return super.get(id);
    }

    @Override
    public List<CmsContentType> findList(CmsContentType cmsContentType) {
        return super.findList(cmsContentType);
    }

    @Override
    public List<CmsContentType> findAllList(CmsContentType entity) {
        return findList(entity);
    }

    @Override
    public Page<CmsContentType> findPage(Page<CmsContentType> page, CmsContentType cmsContentType) {
        return super.findPage(page, cmsContentType);
    }

    @Override
    @Transactional(readOnly = false)
    public void save(CmsContentType cmsContentType) {
        // 设置siteId
        String siteId = "";
        if (null != cmsContentType.getColumn()) {
            CmsColumn cmsColumn = CmsUtils.getColumnById(cmsContentType.getColumn().getId());
            siteId = null != cmsColumn ? cmsColumn.getSiteId() : null;
            cmsContentType.setSiteId(siteId);
        }

        super.save(cmsContentType);

        // 清除缓存
        CmsCacheUtils.removeCacheBySiteId(CmsCacheUtils.CONTENT_TYPE_LIST_CACHE, siteId);
    }

    @Override
    @Transactional(readOnly = false)
    public void delete(CmsContentType cmsContentType) {
        String siteId = get(cmsContentType.getId()).getSiteId();
        super.delete(cmsContentType);

        // 清除缓存
        CmsCacheUtils.removeCacheBySiteId(CmsCacheUtils.CONTENT_TYPE_LIST_CACHE, siteId);
    }

}
